package app.goods.info.utils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import app.goods.info.constants.ConstontValue;
import app.goods.info.constants.GlobalCache;
import app.goods.info.constants.UrlConstont;
import app.goods.info.services.LoginTokenService;
import app.goods.info.services.domain.ShopInfo;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class ShopInfoUtils {
//	private static final Logger LOGGER = LogManager.getLogger(ShopInfoUtils.class);

	public static JSONObject getShopInfo(String shopNo) {
		JSONObject result = null;
		String queryUrl = UrlConstont.SHOP_INFO_URL.replace("<shopNo>", shopNo);
		String responseBody = HttpClientUtils.get(UrlUtils.getRequestUrl(queryUrl), UrlUtils.builQueryParameters());
		if (StringUtils.isNotEmpty(responseBody)) {
			result = JSONUtil.parseObj(responseBody);
		}

		return result;
	}

	public static void getAllShopInfo() {
		JSONObject result = null;
		JSONArray allShops = new JSONArray();
		for (int i = 115; i < 886; i++) {
			result = getShopInfo(String.valueOf(i));
			System.out.println("====" + i + "====" + result == null ? "null" : result.toString());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (result == null || !result.getStr("code").equals("0")) {
				continue;
			}

			JSONObject shopInfo = result.getJSONObject("result");
			allShops.add(shopInfo);
			System.out.println(result.getJSONObject("result").getStr("shop_no") + "==="
					+ result.getJSONObject("result").getStr("shop_name"));
		}
		FileReaderUtils.writeJsonToFileByLine(allShops.toStringPretty(), "/configs/target_all_shops11.json");
		FileReaderUtils.writeJsonToFileByLine(allShops.toStringPretty(), "/configs/target_all_shops.json");

	}

	public static void main(String[] args) {

//		LoginTokenService loginTokenService = new LoginTokenService();
//		String token = loginTokenService.getToken();
		GlobalCache.getInstance().setLoginToken("9CDED303E5651DCFFEA5E7C1D7A2B270");
		getAllShopInfo();
//		List<ShopInfo> targetInfosFinal = new ArrayList<ShopInfo>();
//		List<ShopInfo> targetInfos = new ArrayList<ShopInfo>();
//		String jsonString = FileReaderUtils.readFileQuikly(ConstontValue.TARGET_SHOP_INFO);
//		targetInfos.addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), ShopInfo.class));
//		targetInfos.sort(new Comparator<ShopInfo>() {
//
//			@Override
//			public int compare(ShopInfo o1, ShopInfo o2) {
//				// TODO Auto-generated method stub
//				return o1.getShopNo().compareTo(o2.getShopNo());
//			}
//		});
//		FileReaderUtils.writeJsonToFileByLine(JSONUtil.toJsonPrettyStr(targetInfos), ConstontValue.TARGET_SHOP_INFO);
//		
//		List<String> shops = FileReaderUtils.readFileByLines("configs/all_shaops.txt");
//		shops.forEach(str->{
//			String[] strs= str.split("===");
//			ShopInfo shop = new ShopInfo(strs[1],strs[0],ConstontValue.OFF);
//			
//			if(targetInfos.contains(shop)) {
//				shop.setStatus(ConstontValue.ON);
//			}
//			targetInfosFinal.add(shop);
//		});
//		FileReaderUtils.writeJsonToFileByLine(JSONUtil.toJsonPrettyStr(targetInfosFinal), "configs/all_shops.json");
	}
}
